<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" @clear="searchCard" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" @clear="searchCard" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option v-for="item in options" :key="item.id" :value="item.value" :label="item.label" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="searchCard">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addCard')">添加月卡</el-button>
      <el-button @click="batchDel">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="statusFormatter" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="renew(row.id)">续费</el-button>
            <el-button size="mini" type="text" @click="cardDetail(row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="cardEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="cardDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口
import { getCardListAPI } from '@/api/card'
import { deleteCardAPI } from '@/api/addCard'
export default {
  data() {
    return {
      // 状态
      ids: null,
      options: [
        {
          id: 0,
          value: 0,
          label: '可用'
        },
        {
          id: 1,
          value: 1,
          label: '已过期'
        }],
      // 列表数据
      list: [],
      // 查询参数
      params: {
        page: 1,
        pageSize: 2,
        personName: null,
        carNumber: null,
        cardStatus: null
      },
      total: 0
    }
  },
  // 渲染之后
  async mounted() {
    // 获取搜索列表数据
    await this.searchCard()
    // 获取列表数据
    await this.getCardList()
    // 删除月卡
  },
  // 方法
  methods: {
    // 渲染列表
    async getCardList() {
      const res = await getCardListAPI(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 分页,点击下一页并重新渲染列表
    handleCurrentChange(value) {
      // 获取当前页码，重新渲染列表
      this.params.page = value
      this.getCardList()
    },
    // 格式化列表
    statusFormatter(row) {
      const MAP = {
        0: '可用',
        1: '已过期'
      }
      return MAP[row.cardStatus]
    },
    // 查询列表
    async  searchCard() {
      this.getCardList()
      // 为了防止分页出错，每次查询都重置页码为1
      this.params.page = 1
    },
    // 跳转到编辑并携带ID
    cardEdit(id) {
      this.$router.push({
        path: '/addCard',
        query: {
          id
        }
      })
    },
    // 续费月卡,跳转到续费月卡带ID
    renew(id) {
      this.$router.push({
        path: '/renewCard',
        query: {
          id
        }
      })
    },
    // 查看月卡并携带ID跳转到查看页面
    cardDetail(id) {
      this.$router.push({
        path: '/examineCard',
        query: {
          id
        }
      })
    },
    // 删除月卡
    cardDel(id) {
      // 删除月卡
      // 是否确定删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用接口
        await deleteCardAPI(id)
        // 重新渲染列表
        this.getCardList()
        // 弹出提示
        this.$message({
          type: 'success',
          message: '删除成功!'

        })
      }).catch(() => {

      })
    },
    // 批量删除月卡
    async batchDel() {
      // 判断是否有id，有ID才删除
      if (this.ids) {
        // 调用接口
        await deleteCardAPI(this.ids)
        // 批量删除成功后在重新渲染列表
        this.getCardList()
        // 弹出提示
        this.$message.success('批量删除成功')
      }
    },
    // 批量删除，获取选中的数据id，在把id传给this.ids,判断是否选中数据
    handleSelectionChange(val) {
      const ids = val.map(item => item.id).join(',')
      this.ids = ids
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
